package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.CommonDao;
import cn.edu.sdjzu.xg.bysj.dao.SchoolDao;

import cn.edu.sdjzu.xg.bysj.domain.School;

import exception.BysjException;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;


import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class SchoolService {
    private static SchoolDao schoolDao = SchoolDao.getInstance();
    private static SchoolService schoolService = new SchoolService();

    public static SchoolService getInstance() {
        return schoolService;
    }

    public Collection<School> findAll
            (List<Condition> conditionList_json_str, Pagination pagination)
            throws SQLException {

        Connection connection = JdbcHelper.getConn();
        Collection<School> schools = SchoolDao.findAll(connection, conditionList_json_str, pagination);
        JdbcHelper.close(connection);
        return schools;
    }

    public School find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        School result = schoolDao.find(id, connection);
        JdbcHelper.close(connection);
        return result;
    }

    public boolean update(School school) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean result = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            result = schoolDao.update(school, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return result;
    }

    public boolean add(School school) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean result = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            result = schoolDao.add(school, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return result;
    }

    public boolean delete(Integer id) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        School school = this.find(id);
        boolean result = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            result = this.delete(school, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return result;
    }

    public boolean delete(School school_json, Connection connection) throws SQLException, BysjException {

        boolean result = false;
        int countTeacherOfProfTitle = CommonDao.getInstance().
                countWhereForeignKey(connection, "department", "school_id", school_json.getId());
        //若没有二级单位，则能够删除
        if (countTeacherOfProfTitle == 0) {
            result = schoolDao.delete(school_json, connection);
        } else {
            throw new BysjException(school_json.getDescription() + "有下属系，不能删除");
        }
        JdbcHelper.close(connection);
        return result;
    }

}
